package com.mdc.mansion.core.modules.basedata.impl;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.mdc.mansion.core.modules.basedata.TableQrCodeService;
import com.mdc.mansion.core.modules.restaurant.RestaurantTableService;
import com.mdc.mansion.core.modules.system.SysDictService;
import com.mdc.mansion.dataaccess.common.service.CrudService;
import com.mdc.mansion.dataaccess.modules.basedata.dao.TableQrCodeDao;
import com.mdc.mansion.dataaccess.modules.basedata.entity.TableQrCode;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.RestaurantTable;
import com.mdc.mansion.dataaccess.modules.system.entity.Dict;

/**
 * 餐桌二维码信息ServiceImpl
 * @author mingdongc
 * @version 2018-01-01
 */
@Service
public class TableQrCodeServiceImpl extends CrudService<TableQrCodeDao, TableQrCode> implements TableQrCodeService {
	
	@Autowired
	private RestaurantTableService restaurantTableService;
	
	// 二维码尺寸
	private static final int QRCODE_SIZE = 500;
	
    private static final String DICT_QR_HOST = "DICT_QR_HOST";
    @Autowired
    private SysDictService sysDictService;
    @Override
    public String createQrCodeUrl(String qrCodeSign) {
        return "/getQrCode?codeSign="+qrCodeSign;
    }

    @Override
    public BufferedImage createImgQrCode(TableQrCode tableQrCode) throws Exception {
        String content = tableQrCode.getUrl();// 内容  
        int width = 500; // 图像宽度  
        int height = 610; // 图像高度  
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();  
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");  
        
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content,  
                BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵  
  
        ///生成图片
        BufferedImage source = MatrixToImageWriter.toBufferedImage(bitMatrix);
        RestaurantTable restaurantTable = new RestaurantTable();
        restaurantTable.setQrCodeSign(tableQrCode.getCodeSign());
        List<RestaurantTable> tableList = restaurantTableService.findList(restaurantTable);
        if(null == tableList || 0 == tableList.size())
        {
        	//添加文字
            addFontImage(source,null,tableQrCode);
        }else
        {
        	//添加文字
            addFontImage(source,tableList.get(0),tableQrCode);
        }
        
        return source;
    }
    
    
    private void addFontImage(BufferedImage source,RestaurantTable restaurantTable,TableQrCode tableQrCode)
    {
    	
    	Graphics g = source.createGraphics();
    	g.drawImage(source, 0, 0, 500, 500, null);
    	g.setColor(Color.BLACK);
    	g.setFont(new Font("宋体", Font.CENTER_BASELINE, 20));
    	if( null == restaurantTable)
    	{
    		g.drawString("ID:"+tableQrCode.getCodeSign(), 60, 450);
    		g.drawString("Name:", 60, 480);
    		g.drawString("请扫码自助点餐<haohey>", 60, 540);
        	g.drawString("Please scan code buffet <haohey>", 60, 570);
    	}else
    	{
    		g.drawString("ID:"+restaurantTable.getQrCodeSign(), 60, 450);
    		g.drawString("Name: "+restaurantTable.getChineseName() , 60, 480);
    		g.drawString("      "+restaurantTable.getEnglishName(), 60, 520);
    		g.drawString("请扫码自助点餐<haohey>", 60, 545);
        	g.drawString("Please scan code buffet <haohey>", 60, 570);
    	}
    	g.dispose();
    	
    }

    @Transactional
    @Override
    public List<BufferedImage> batchCreateQrCode(List<TableQrCode> tableCodeList) throws Exception {
        List<BufferedImage> list = new ArrayList<>();
        for(TableQrCode tableQrCode : tableCodeList){
            list.add(this.createImgQrCode(tableQrCode));
            this.save(tableQrCode);
        }
        return list;
    }

    @Override
    public String getQrHost() {
        Dict dict = new Dict();
        dict.setType(DICT_QR_HOST);
        List<Dict> list = sysDictService.findList(dict);
        String host = "";
        if(list!=null&&list.size()>0){
            host = list.get(0).getValue();
        }
        return host;
    }
}
